package jym.tan.movielibrary.configuration.ui;

import java.awt.Frame;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.SwingUtilities;
import javax.swing.UnsupportedLookAndFeelException;

import com.jtattoo.plaf.acryl.AcrylLookAndFeel;
import com.jtattoo.plaf.aero.AeroLookAndFeel;
import com.jtattoo.plaf.aluminium.AluminiumLookAndFeel;
import com.jtattoo.plaf.bernstein.BernsteinLookAndFeel;
import com.jtattoo.plaf.fast.FastLookAndFeel;
import com.jtattoo.plaf.graphite.GraphiteLookAndFeel;
import com.jtattoo.plaf.hifi.HiFiLookAndFeel;
import com.jtattoo.plaf.luna.LunaLookAndFeel;
import com.jtattoo.plaf.mcwin.McWinLookAndFeel;
import com.jtattoo.plaf.mint.MintLookAndFeel;
import com.jtattoo.plaf.noire.NoireLookAndFeel;
import com.jtattoo.plaf.smart.SmartLookAndFeel;

import jym.tan.movielibrary.constant.ThemeConstant;
import jym.tan.movielibrary.factory.ComponentFactory;
import jym.tan.movielibrary.factory.FileFactory;
import jym.tan.movielibrary.logger.LibraryLogger;
import jym.tan.movielibrary.model.ThemeModel;

public class UIManagerImpl implements UIManager {
	private UIProperties uiProperties;

	public UIManagerImpl() {

	}

	public void loadUIProperties() throws FileNotFoundException, IOException, ClassNotFoundException {
		UIProperties uiProperties = null;
		if (FileFactory.isUIPropertiesExists()) {
			LibraryLogger.infoMessage("UIProperties File Exists.");
			uiProperties = FileFactory.getUIProperties();
		} else {
			uiProperties = new UIProperties(new ThemeModel(ThemeConstant.ThemeName.AERO, ThemeConstant.ThemeClass.PLAF_AERO));
			FileFactory.createUIProperties(uiProperties);
		}
		setUiProperties(uiProperties);
	}

	public void saveUIProperties(UIProperties properties) throws FileNotFoundException, IOException {
		FileFactory.saveUIProperties(properties);
	}

	public void setUiProperties(UIProperties uiProperties) {
		this.uiProperties = uiProperties;
	}

	public UIProperties getUiProperties() {
		return uiProperties;
	}

	public void setDefaultTheme() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
		javax.swing.UIManager.setLookAndFeel(uiProperties.getThemeModel().getThemeClass());
	}

	public void updateDefaultTheme(ThemeModel themeModel) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException, FileNotFoundException, IOException {
		uiProperties.setThemeModel(themeModel);
		updateThemeClass(themeModel);
		javax.swing.UIManager.setLookAndFeel(uiProperties.getThemeModel().getThemeClass());
		ComponentFactory.getLibraryFrame().getRootPane().updateUI();
		for (int i = 0; i < Frame.getWindows().length; i++) {
			SwingUtilities.updateComponentTreeUI(Frame.getWindows()[i]);
		}
		for (int i = 0; i < Frame.getFrames().length; i++) {
			SwingUtilities.updateComponentTreeUI(Frame.getFrames()[i]);
		}
		
		saveUIProperties(uiProperties);
		
	}

	private void updateThemeClass(ThemeModel themeModel) {
		if (themeModel.getThemeClass().equals(ThemeConstant.ThemeClass.PLAF_FAST)) {
			FastLookAndFeel.setTheme("Default");
		} else if (themeModel.getThemeClass().equals(ThemeConstant.ThemeClass.PLAF_GRAPHITE)) {
			GraphiteLookAndFeel.setTheme("Default");
		} else if (themeModel.getThemeClass().equals(ThemeConstant.ThemeClass.PLAF_SMART)) {
			SmartLookAndFeel.setTheme("Default");
		} else if (themeModel.getThemeClass().equals(ThemeConstant.ThemeClass.PLAF_ACRYL)) {
			AcrylLookAndFeel.setTheme("Default");
		} else if (themeModel.getThemeClass().equals(ThemeConstant.ThemeClass.PLAF_AERO)) {
			AeroLookAndFeel.setTheme("Default");
		} else if (themeModel.getThemeClass().equals(ThemeConstant.ThemeClass.PLAF_BERNSTEIN)) {
			BernsteinLookAndFeel.setTheme("Default");
		} else if (themeModel.getThemeClass().equals(ThemeConstant.ThemeClass.PLAF_ALUMINIUM)) {
			AluminiumLookAndFeel.setTheme("Default");
		} else if (themeModel.getThemeClass().equals(ThemeConstant.ThemeClass.PLAF_MCWIN)) {
			McWinLookAndFeel.setTheme("Default");
		} else if (themeModel.getThemeClass().equals(ThemeConstant.ThemeClass.PLAF_MINT)) {
			MintLookAndFeel.setTheme("Default");
		} else if (themeModel.getThemeClass().equals(ThemeConstant.ThemeClass.PLAF_HIFI)) {
			HiFiLookAndFeel.setTheme("Default");
		} else if (themeModel.getThemeClass().equals(ThemeConstant.ThemeClass.PLAF_NOIRE)) {
			NoireLookAndFeel.setTheme("Default");
		} else if (themeModel.getThemeClass().equals(ThemeConstant.ThemeClass.PLAF_LUNA)) {
			LunaLookAndFeel.setTheme("Default");
		}
	}
}
